package instructions;
import core.Memory;
import core.Process;
import operands.Modifier;
import operands.Operand;


public class MovInstruction extends Instruction 
{
	public MovInstruction(Memory memory, Modifier m, Operand a, Operand b) 
	{
		super(memory, m, a, b);
	}

	@Override
	protected void executeInstruction(Process process) 
	{
	    int a_val = a.resolve();
	    int b_val = b.resolve();
	    
		switch(m)
		{
			case I:
			case NON:
				memory.setInstruction(b_val, memory.getInstruction(a_val).copy());
				break;
			case A:
				memory.getInstruction(b_val).setOperandA(memory.getInstruction(a_val).getOperandA().copy());
				break;
			case B:
				memory.getInstruction(b_val).setOperandB(memory.getInstruction(a_val).getOperandB().copy());
				break;
			case AB:
				memory.getInstruction(b_val).setOperandB(memory.getInstruction(a_val).getOperandA().copy());
				break;
			case BA:
				memory.getInstruction(b_val).setOperandA(memory.getInstruction(a_val).getOperandB().copy());
				break;
			case F:
				memory.getInstruction(b_val).setOperandA(memory.getInstruction(a_val).getOperandA().copy());
				memory.getInstruction(b_val).setOperandB(memory.getInstruction(a_val).getOperandB().copy());
				break;
			case X:
				memory.getInstruction(b_val).setOperandB(memory.getInstruction(a_val).getOperandA().copy());
				memory.getInstruction(b_val).setOperandA(memory.getInstruction(a_val).getOperandB().copy());
				break;				
		}
		process.setInstructionPointer(process.getInstructionPointer() + 1);
	}

	@Override
	public Instruction copy() 
	{
		return new MovInstruction(memory, m, a.copy(), b.copy());
	}
}
